from flask import Blueprint
from flask import request, current_app
from flask_jwt_extended import create_access_token, create_refresh_token, jwt_required, get_jwt_identity
from shared import db, bcrypt
from http_status import HttpStatus
import utils
from models import Role

roles_bp = Blueprint('roles', __name__)

@roles_bp.route('add', methods=['POST'])
@jwt_required()
def add_role():
    """添加角色"""
    user_id =  get_jwt_identity()
    data = request.get_json()
    role_name = data.get('role_name')
    role_age = data.get('role_age')
    role_sex = data.get('role_sex')
    role_description = data.get('role_description')
    role_avatar = data.get('role_avatar')
    if not role_name or not role_age or not role_sex or not role_description or not role_avatar:
        return utils.response(False, None, '参数错误', HttpStatus.CODE_PARAM_ERROR)
    
    role_check = Role.query.filter_by(role_name=role_name,deleted_at=None).count()
    if role_check > 0:
        return utils.response(False, None, '角色名称已存在', HttpStatus.CODE_PARAM_ERROR)
    
    role = Role(role_name=role_name, role_age=role_age, role_sex=role_sex, role_description=role_description, role_avatar=role_avatar, user_id=user_id)
    db.session.add(role)
    db.session.commit()

    return utils.response(True, None, '添加成功', HttpStatus.CODE_SUECCESS)

@roles_bp.route('list', methods=['GET'])
@jwt_required()
def get_role_list():
    """获取角色列表"""
    user_id = get_jwt_identity()
    roles = Role.query.filter_by(user_id=user_id,deleted_at=None).all()
    role_dicts = [role.to_dict() for role in roles]
    return utils.response(True, {'list':role_dicts}, '获取成功', HttpStatus.CODE_SUECCESS)

@roles_bp.route('update', methods=['POST'])
@jwt_required()
def update_role():
    """更新角色"""
    data = request.get_json()
    role_id = data.get('role_id')
    role_name = data.get('role_name')
    role_age = data.get('role_age')
    role_sex = data.get('role_sex')
    role_description =data.get('role_description')
    role_avatar = data.get('role_avatar')
    if not role_id or not role_name or not role_age or not role_sex or not role_description or not role_avatar:
        return utils.response(False, None, '参数错误', HttpStatus.CODE_PARAM_ERROR)

    role_check = Role.query.filter_by(role_name=role_name, deleted_at=None).count()
    if role_check > 0:
        return utils.response(False, None, '角色名称已存在', HttpStatus.CODE_PARAM_ERROR)

    role = Role.query.filter_by(id=role_id).first()
    if not role:
        return utils.response(False, None, '角色不存在', HttpStatus.NOT_FOUND)
    
    role.role_name = role_name
    role.role_age = role_age
    role.role_sex = role_sex
    role.role_description = role_description
    role.role_avatar = role_avatar
    db.session.commit()
    return utils.response(True, None, '更新成功', HttpStatus.CODE_SUECCESS)

@roles_bp.route('delete', methods=['POST'])
@jwt_required()
def delete_role():
    """删除角色"""
    data = request.get_json()
    role_id = data.get('role_id')
    if not role_id:
        return utils.response(False, None, '参数错误', HttpStatus.CODE_PARAM_ERROR)
    
    role = Role.query.filter_by(id=role_id).first()
    if not role:
        return utils.response(False, None, '角色不存在', HttpStatus.NOT_FOUND)

    role.deleted_at = utils.time_now()
    db.session.commit()
    return utils.response(True, None, '删除成功', HttpStatus.CODE_SUECCESS)

@roles_bp.route('deleteAll', methods=['POST'])
@jwt_required()
def delete_all_role():
    """删除所有角色"""
    user_id =  get_jwt_identity()
    roles = Role.query.filter_by(user_id=user_id).all()
    for role in roles:
        # 软删除
        role.deleted_at = utils.time_now()
    db.session.commit()
    return utils.response(True, None, '删除成功', HttpStatus.CODE_SUECCESS)
